import { deconstructEvent } from "../../hooks/deconstructEvent";
import http from "../../utils/http";
import Toast from '@vant/weapp/toast/toast';

// pages/form/form.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    identityList: ['群众', '共青团员', '共产党员'],
    index: 0,
    identity: "群众",
    qin1_identity: "群众",
    qin2_identity: "群众",
    qin3_identity: "群众",
    na_place: '北京',
    regionList: [],
    address: "北京市东城区",
    data_grad: "2022-06-30",
    AddressShow: false,
    area_data: {},
    country_data: {},
    country_title: {},
    headerCustomStyle: false, //  顶部样式
    countrydatas: [],
    school_data: [],
    active: 0,
    scl_category_data: [],
    scl_active: -1,
    mj_category_data: [],
    imageUrl: '',
    nid: "",
    enroll_data: {},
    changeBtnShow:false
  },
  // 获取报名表
  getEnroll() {
    const { nid } = this.data
    http.get("/change/enroll/", {
      nid
    }).then(res => {
      console.log(res);
      const { enroll_data } = res
      this.setData({
        enroll_data
      })

    })
  },
  submit(e) {
    const { imageUrl: photo, identity, na_place, address, data_grad, qin1_identity, qin2_identity, qin3_identity,nid } = this.data
    const { value } = e.detail
    let country;
    if (this.data.country_data.country_name) {
      country = this.data.country_data.country_name
    } else {
      country = this.data.enroll_data.country
    }
    console.log(country);
    const upInfo = { ...value, photo, identity, na_place, address, data_grad, qin1_identity, qin2_identity, qin3_identity, country }
    console.log(upInfo);
    // 修改报名表
    if(this.data.changeBtnShow === true){
      http.post("/change/enroll/",{
        nid,
        ...upInfo
      }).then((res) => {
        if(res.success === true){
          Toast.success('修改成功');
          wx.switchTab({
            url:"/pages/index/index"
          })
        }
      })
    }else{
      // 新增报名表
      http.post("/add/enroll/", upInfo).then(res => {
        console.log(res);
        if(res.success === true){
          Toast.success('报名成功，后续会有工作人员联系你');
          wx.switchTab({
            url:"/pages/index/index"
          })
        }
      })
    }
    // upInfo.id = 1
  },
  chooseImage() {
    wx.chooseImage({
      count: 1, // 可选择的图片数量
      sizeType: ['compressed'], // 压缩图片
      sourceType: ['album', 'camera'], // 来源：相册或相机
      success: (res) => {
        this.setData({
          imageUrl: res.tempFilePaths[0]
        });
      }
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', this.data.array[e.detail.value])
  },
  eventhandle(e) {
    const index = e.detail.value
    this.setData({
      identity: this.data.identityList[index]
    })
  },
  eventhandle1(e) {
    const index = e.detail.value
    this.setData({
      qin1_identity: this.data.identityList[index]
    })
  },
  eventhandle2(e) {
    const index = e.detail.value
    this.setData({
      qin2_identity: this.data.identityList[index]
    })
  },
  eventhandle3(e) {
    const index = e.detail.value
    this.setData({
      qin3_identity: this.data.identityList[index]
    })
  },
  eventhandleRegion(e) {
    const { na_place } = deconstructEvent(e)
    console.log(na_place);
    if (na_place === "na_place") {
      this.setData({
        na_place: e.detail.value[0]
      })
      console.log(this.data.na_place);
    } else {
      let address = Object.values(e.detail.value)
      address = address.join()
      console.log(address);
      this.setData({
        address
      })
    }

    console.log(e);
  },
  bindDateChange(e) {
    console.log(e.detail);
    this.setData({
      data_grad: e.detail.value
    })

  },
  //  显示地址选择框
  showAddressPopup() {
    this.setData({
      AddressShow: true,
    });
    console.log("触发了", this.data.AddressShow);
  },
  //onAddressPopupClose
  onAddressPopupClose() {
    this.setData({
      AddressShow: false,
    });
  },
  //  选择地址  一级选择
  onchange(e: any) {
    console.log(e);
    let { selectedOptions } = e.detail;
    let value = selectedOptions[0].area_id
    let countrydatas = this.data.countrydatas;
    let activeAreaIndex = countrydatas.findIndex((v) => {
      return v.area_id === value;
    });
    // console.log(countrydatas)
    if (countrydatas[activeAreaIndex].loaded) return;
    this.get_country(value, activeAreaIndex);
  },
  // 选择地址  最终
  onFinish(e: any) {
    console.log(e)
    let { selectedOptions, tabIndex } = e.detail;
    this.setData({
      country_data: selectedOptions[tabIndex],
    });
    this.onAddressPopupClose();

  },
  onPageScroll(e) {
    this.setData({
      headerCustomStyle: e.scrollTop > 20,
    });
  },
  onNavChange(e) {
    // console.log(e);
    this.setData({
      active: e.detail.index,
    });
  },
  //  获取地区数据
  get_area() {
    http.get('/get/area/').then((res) => {
      console.log(res, "地区")
      let countrydatas = res.area_data.map((v) => {
        console.log(v, "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV");
        v.text = v.area_name;
        v.value = v.area_id;
        v.children = [];
        v.loaded = false;
        return v;
      });
      this.setData({
        countrydatas,
      })
      console.log(this.data.countrydatas, "countrydatas");

    }).catch(v => {
      console.log(v)
    })
  },
  //  获取国家数据
  get_country(area_id, index) {
    http
      .get('/get/country/', {
        area_id,
      })
      .then((res) => {
        // console.log(res);
        let countrydatas = this.data.countrydatas;
        console.log(countrydatas);
        let data = res.country_data.map((v) => {
          v.text = v.country_name;
          v.value = v.country_id;
          return v;
        });
        // console.log(data);
        let key1 = 'countrydatas[' + index + '].children';
        let key2 = 'countrydatas[' + index + '].loaded';
        console.log(key1, "kkkkkkkkkk");
        console.log(key2, "kkkkkkkkkk");

        // console.log(key1);
        this.setData({
          [key1]: data,
          [key2]: true,
        });
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(o) {
    console.log(o);
    const { nid } = o
    if (nid !== undefined) {
      this.setData({
        nid,
        changeBtnShow:true
      })
      this.getEnroll()
    }
    this.get_area()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})